package cn.raven.user.controller;

import cn.raven.basic.annotation.PreAuthorize;
import cn.raven.basic.utils.AjaxResult;
import cn.raven.basic.utils.PageList;
import cn.raven.user.domain.Logininfo;
import cn.raven.user.query.LogininfoQuery;
import cn.raven.user.service.ILogininfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**

* @Description: 登录接口类
* @Author: king
* @Version: 1.0
* @Date:  2022/6/24 10:10  
  */
@RestController
@RequestMapping("/loginfo")
@Api(value = "登录的API",description="登录相关的CRUD功能")
public class LogininfoController {
    @Autowired
    private ILogininfoService logininfoService;

    /**
    * @Title: findAll
    * @Description: 查询登录所有
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/6/24 9:56
    * @Parameters: []
    * @Return java.util.List<cn.raven.user.domain.Logininfo>
      */
    @GetMapping
    private List<Logininfo> findAll(){
        return logininfoService.findAll();
    }

    /**
    * @Title: findById
    * @Description: 根据id查询数据
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/6/24 9:57
    * @Parameters: [id]
    * @Return cn.raven.user.domain.Logininfo
      */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(name = "id查询登录信息",sn = "loginfo:id")
    public Logininfo findById(@ApiParam(value="根据id查询",required = true) @PathVariable("id") Long id){
        return logininfoService.findOne(id);
    }

    /**
    * @Title: del
    * @Description: 根据id删除数据
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/6/24 9:59
    * @Parameters: [id]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除登录信息",sn = "loginfo:delete")
    public AjaxResult del(@PathVariable("id") Long id){
        logininfoService.del(id);
        return AjaxResult.me();
    }
    
    /** 
    * @Title: save
    * @Description: 新增/修改
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/6/24 10:01  
    * @Parameters: [logininfo]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PutMapping
    @PreAuthorize(name = "保存登录信息",sn = "loginfo:save")
    public AjaxResult save(@RequestBody Logininfo logininfo){
        try {
            if (logininfo.getId() == null){
                logininfoService.add(logininfo);
            }else {
                logininfoService.update(logininfo);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请骚后重试").setSuccess(false);
        }
    }
    
    /** 
    * @Title: queryDate
    * @Description: 分页+高级查询
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/6/24 10:34  
    * @Parameters: [logininfoQuery]
    * @Return cn.raven.basic.utils.PageList<cn.raven.user.domain.Logininfo>
      */
    @PostMapping
    @PreAuthorize(name = "登录信息列表",sn = "loginfo:list")
    public PageList<Logininfo> queryDate(@RequestBody LogininfoQuery logininfoQuery){
        return logininfoService.queryData(logininfoQuery);
    }
    
    /** 
    * @Title: batchDel
    * @Description: 根据ids 批量删除
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/6/25 12:29
    * @Parameters: [ids]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PatchMapping
    @PreAuthorize(name = "批量删除登录信息",sn = "loginfo:batchDelete")
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            logininfoService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请骚后重试").setSuccess(false);
        }
    }
}